package com.goktech.sportsmall.service.Impl;

import com.goktech.sportsmall.bean.LoginBean;
import com.goktech.sportsmall.bean.UserBean;
import com.goktech.sportsmall.mapper.LoginMapper;
import com.goktech.sportsmall.mapper.UserMapper;
import com.goktech.sportsmall.service.UserService;

import com.goktech.sportsmall.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private LoginMapper loginMapper;

    // 查询数据库中是否存在对应的联系电话
    @Override
    public int selectUserPhone(String tel) {
        return userMapper.selectUserPhone(tel);
    }

    // 查询数据库中存在的联系电话的信息
    @Override
    public UserBean selectUserPhoneInfo(String tel) {
        return userMapper.selectUserPhoneInfo(tel);
    }

    @Override
    public String RegisterUser(UserBean userBean) {
        userBean.setuBirthDate(DateUtil.getDate("yyyyMMdd"));
        userMapper.insertUser(userBean);
        LoginBean loginBean = new LoginBean();
        loginBean.setAluId(userBean.getU_id());
        loginBean.setlAccount(userBean.getAccount());
        loginBean.setlPassword(userBean.getPassword());
        loginBean.setlType(1);
        loginBean.setlCreateTime(DateUtil.getDate("yyyyMMddHHmmss"));
        loginMapper.insertUser(loginBean);
        return "注册成功";
    }


//    @Override
//    public String RegisterUser(UserBean userBean) {
//        userMapper.insertUser(userBean);
//        return "注册成功";
//    }

    @Override
    public Map<String,Object> selectList(int page, int size) {
        Map<String,Object> restMap = new HashMap<>();
        int count = userMapper.selectCount();
        int total=0;
        if(count%size==0){
            total = count/size;
        }else{
            total = count/size+1;
        }
        restMap.put("count",count);
        restMap.put("page",page);
        restMap.put("total",total);
        restMap.put("userList",userMapper.selectList((page-1)*size,size));
        return restMap;
    }

    // 删除
    @Override
    public String deleteUser(String UID) {
        int result = userMapper.deleteUser(UID);
        if (result==1){
            return "删除成功";
        }
        return "删除失败";
    }

    // 修改
    @Override
    public String updateUser(UserBean userBean) {
        int result = userMapper.updateUser(userBean);
        if (result == 1){
            return "修改成功";
        }
        return "修改失败";
    }

    // 添加信息
    @Override
    public String insertUser(UserBean userBean) {
        userBean.setuCreateTime(DateUtil.getDate("yyyyMMddHHmmss"));
        int result = userMapper.insertUser(userBean);
        if (result == 1){
            return "添加成功";
        }
        return "添加失败";
    }
}
